var article_apis = {
	postDetail: "http://api.fangchepingtai.com/fangchebbs/post/detail/",
	createrDetail: "http://api.fangchepingtai.com/fangchebbs/user/info/",
	replylist: "http://api.fangchepingtai.com/fangchebbs/reply/list",
	replycount: "http://api.fangchepingtai.com/fangchebbs/reply/count",
	createreply: "http://api.fangchepingtai.com/fangchebbs/reply/create",
};
(function() {
	var post_id, page, num, pages;

	// 新建回复
	App.set("$Event.article.post_reply", function() {
		var data = {
			content: App.get("$Cache.article.reply.new.content"),
			replyer: App.get("$Cache.article.creater"),
			post_name: App.get("$Cache.article.post_detail.title"),
			post_id: App.get("$Cache.article.post_detail._id"),
			_module: App.get("$Cache.article.post_detail._module"),
			_module_name: App.get("$Cache.article.post_detail._module_name")
		};
		coAjax.post(article_apis.createreply, data, function(result) {
			console.log(result);
			alert("success", "回复成功");
			App.set("$Cache.article.reply.new.content", "");
			App.unshift("$Cache.article.replylist", result);
			document.body.scrollTop = 0;
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	});

	// 获取回复列表
	function getReplyList(post_id, page, num) {
		coAjax.get(article_apis.replylist, {
			post_id: post_id,
			page: page,
			num: num
		}, function(result) {
			console.log(result);
			App.set("$Cache.article.replylist", result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	};

	// 翻页
	App.set("$Event.article.preReplyPage", function() {
		var jumpPage = page;
		jumpPage > 0 ? jumpPage-- : jumpPage = 0;
		Path.jump("/article.html?_id=" + App.get("$Cache.article.current_post_id") + "&page=" + jumpPage + "&num=" + num);
	});
	App.set("$Event.article.nextReplyPage", function() {
		var jumpPage = page;
		jumpPage < pages.length - 1 ? jumpPage++ : jumpPage = pages.length - 1;
		Path.jump("/article.html?_id=" + App.get("$Cache.article.current_post_id") + "&page=" + jumpPage + "&num=" + num);
	});

	Path.on("/__basename__.html", function(_current_location) {
		var qs = _current_location.query;
		post_id = qs.get("_id");
		App.set("$Cache.article.current_post_id", post_id);
		page = qs.get("page") || 0;
		App.set("$Cache.article.current_reply_page", page);
		num = qs.get("num") || 10;
		App.set("$Cache.article.page_num", num);

		// 获取话题详情
		coAjax.get(article_apis.postDetail + post_id, function(result) {
			// console.log(result);
			App.set("$Cache.article.post_detail", result);
			// 获取话题创建者信息
			coAjax.get(article_apis.createrDetail + result.creater_id, function(creater) {
				console.log(creater);
				App.set("$Cache.article.creater", creater);
			}, function(err, errMsg, errCode) {
				console.log(err);
				console.log(errMsg);
			});
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});

		// 获取回复统计
		coAjax.get(article_apis.replycount, {
			post_id: post_id
		}, function(result) {
			console.log(result);
			pages = [];
			pages.length = Math.ceil(result / num) || 1;
			App.set("$Cache.article.replypages", pages);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
		getReplyList(post_id, page, num);
	});

}());